<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>组件生命周期（旧）</title>
</head>
<body>
    <div id="app"></div>

    <script type="text/javascript" src="../js/react.development.js"></script>
    <script type="text/javascript" src="../js/react-dom.development.js"></script>
    <script type="text/javascript" src="../js/babel.min.js"></script>

    <script type="text/babel">
        class Life extends React.Component {
            state = {opacity:1}
            death = () => {
                // 卸载组件
                ReactDOM.unmountComponentAtNode(document.querySelector('#app'))
            }
            // 组件被挂载
            componentDidMount() {
                this.timer = setInterval(() => {
                    // 获取原状态
                    let {opacity}= this.state
                    opacity -= 0.1
                    console.log(opacity);
                    if(opacity <= 0) {
                        opacity = 1
                    }
                    this.setState({opacity})
                },200)
            }
            // 组件被卸载
            componentWillUnmount() {
                clearInterval(this.timer)
            }
            render() {
                return (
                    <div id="">
                        <h1 style={{opacity:this.state.opacity}}>***</h1>
                        <button onClick={this.death}>关闭</button>
                    </div>
                )
            }
        }

        ReactDOM.render(<Life/>,document.querySelector("#app"))

    </script>
</body>
</html>